﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Micrhard.FMCG.Server.Models;
using Micrhard.FMCG.Server.Interface;
using Microhard.Core;
using Microhard.Core.Security;
using Microhard.Permission.Interface;

namespace Microhard.FMCG.WebUI.Areas.ClientService
{
    /// <summary>
    /// Summary description for ToolService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class ToolService : System.Web.Services.WebService
    {
        [WebMethod]
        public EmployeeModelSvcModel EmployeeLogin(string userName, string pwd)
        {
            EmployeeModelSvcModel result = null;
            string decUserName = EncDec.Decrypt(userName, System.Configuration.ConfigurationManager.AppSettings["ServiceToken"]);
            string decpwd = EncDec.Decrypt(pwd, System.Configuration.ConfigurationManager.AppSettings["ServiceToken"]);
            IUser iu = IoC.Resolve<IUser>();
            if (iu.ValidatUser(decUserName, decpwd))
            {
                IEmployee iemp = IoC.Resolve<IEmployee>();
                var emp = iemp.GetByUserName(decUserName);
                result = new EmployeeModelSvcModel
                {
                    CompanyID = emp.CompanyID,
                    Name = emp.Name,
                    Range = emp.Range,
                    RangeDisplay = emp.RangeDisplay
                };
            }
            return result;
        }

        //[WebMethod]
        //public List<OrderModel> GetNewOrder(string serviceToken, List<OrderModel> parseOrders)
        //{
        //    string companyID = EncDec.Decrypt(serviceToken, System.Configuration.ConfigurationManager.AppSettings["ServiceToken"]);
        //    if (string.IsNullOrEmpty(companyID))
        //        return null;
        //    Guid comID = Guid.Parse(companyID);
        //    ICompany icom = IoC.Resolve<ICompany>();
        //    var cmp = icom.GetModelByID(comID);
        //    if (cmp == null)
        //        return null;
        //    IOrder iorder = IoC.Resolve<IOrder>();
        //    parseOrders.ForEach(o =>
        //    {
        //        o.CompanyID = comID;
        //    });
        //    return iorder.GetNew(comID, parseOrders);
        //}

        [WebMethod]
        public List<NewsModel> GetNewNews(string serviceToken, List<NewsModel> parseOrders)
        {
            string companyID = EncDec.Decrypt(serviceToken, System.Configuration.ConfigurationManager.AppSettings["ServiceToken"]);
            if (string.IsNullOrEmpty(companyID))
                return null;
            Guid comID = Guid.Parse(companyID);
            ICompany icom = IoC.Resolve<ICompany>();
            var cmp = icom.GetModelByID(comID);
            if (cmp == null)
                return null;
            INews inews = IoC.Resolve<INews>();
            parseOrders.ForEach(o =>
            {
                o.CompanyID = comID;
            });
            return inews.GetNew(parseOrders);
        }

        [WebMethod]
        public List<EmployeeModelSvcModel> GetEmployee(string serviceToken)
        {
            string companyID = EncDec.Decrypt(serviceToken, System.Configuration.ConfigurationManager.AppSettings["ServiceToken"]);
            if (string.IsNullOrEmpty(companyID))
                return null;
            Guid comID = Guid.Parse(companyID);
            IEmployee iemp = IoC.Resolve<IEmployee>();
            var emps = iemp.Query(new EmployeeModel { CompanyID = comID });
            if (emps != null)
            {
                return emps.Select(e => new EmployeeModelSvcModel
                {
                    Cellphone = e.Cellphone,
                    Name = e.Name,
                    QQMail = e.QQMail,
                    Range = e.Range
                }).ToList();
            }
            return null;
        }

        [WebMethod]
        public Dictionary<string, string> Shakehands()
        {
            return null;
        }

        [WebMethod]
        public bool UpdateNewsDetail(string serviceToken, List<NewsModel> news)
        {
            string companyID = EncDec.Decrypt(serviceToken, System.Configuration.ConfigurationManager.AppSettings["ServiceToken"]);
            if (string.IsNullOrEmpty(companyID))
                return false;
            Guid comID = Guid.Parse(companyID);
            INews inews = IoC.Resolve<INews>();
            news.ForEach(o =>
            {
                o.CompanyID = comID;
            });
            var result = inews.UpdateNewsDetail(news);
            return result.IsSucceed;
        }

        [WebMethod]
        public void UpdateSaleStatement(string serviceToken, List<StatementModelInServer> StatementModelsInServer)
        {
            string companyID = EncDec.Decrypt(serviceToken, System.Configuration.ConfigurationManager.AppSettings["ServiceToken"]);
            if (string.IsNullOrEmpty(companyID))
                return;
            Guid comID = Guid.Parse(companyID);
            IOrderStatement ios = IoC.Resolve<IOrderStatement>();
            //赋予公司id
            StatementModelsInServer.ForEach(i =>
            {
                i.CompanyID = comID;
            });
            ios.AddInBatch(StatementModelsInServer);
        }

        [WebMethod]
        public void UpdateOrderRecTime(string serviceToken, List<OrderModel> orderCode)
        {
            string companyID = EncDec.Decrypt(serviceToken, System.Configuration.ConfigurationManager.AppSettings["ServiceToken"]);
            if (string.IsNullOrEmpty(companyID))
                return;
            Guid comID = Guid.Parse(companyID);
            IOrder ios = IoC.Resolve<IOrder>();
            ios.UpdateReceiveGoods(comID, orderCode);
        }

        [WebMethod]
        public void UpdateOrderDetail(string serviceToken, string orderCode, List<OrderDetailModelInServer> orderDetail, string kaCode)
        {
            string companyID = EncDec.Decrypt(serviceToken, System.Configuration.ConfigurationManager.AppSettings["ServiceToken"]);
            if (string.IsNullOrEmpty(companyID))
                return;
            Guid comID = Guid.Parse(companyID);
            IOrder ios = IoC.Resolve<IOrder>();
            ios.SaveDetail(comID, orderCode, orderDetail, kaCode);
        }
    }
}
